;; -*- emacs-lisp -*-

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ECB Setting ;;;;;;;;;;;;;;;;;;
;(autoload 'speedbar-frame-mode "speedbar" "Popup a speedbar frame" t)
;(autoload 'speedbar-get-focus "speedbar" "Jump to speedbar frame" t)
;(global-set-key [(f4)] 'speedbar-get-focus)
;(eval-after-load "info" '(require 'sb-info))

;(add-to-list 'load-path "~/.emacs.d/speedbar-0.14beta4")
;(add-to-list 'load-path "~/.emacs.d/eieio-0.17")
;(add-to-list 'load-path "~/.emacs.d/ecb-2.32")
;(add-to-list 'load-path "~/.emacs.d/semantic-1.4.4")
;(add-to-list 'load-path "~/.emacs.d/cedet-1.0pre3/common")
;(add-to-list 'load-path "~/.emacs.d/elib-1.0")
;(require 'ede)
;(require 'ecb)

;(load-file (expand-file-name "~/.emacs.d/cedet-1.0beta2b/common/cedet.el"))
;(load-file "~/.emacs.d/cedet-1.0pre3/common/cedet.el")
;(require 'jde)

;(require 'semantic-c)
;(require 'semantic-el)
;(require 'semantic-make)
;(require 'semantic-imenu)
;(add-hook 'speedbar-load-hook
;  (lambda () (require 'semantic-sb)))
;(autoload 'semantic-bnf-mode "semantic-bnf"
;  "Mode for Bovine Normal Form." t)
;(add-to-list 'auto-mode-alist
;  '("\.bnf$" . semantic-bnf-mode))
;(autoload 'semantic-minor-mode "semantic-mode"
;"Mode managing semantic parsing." t)

;(require 'backup-dir)
;(require 'ctypes)
;(require 'setnu-plus)
;(require 'display-line-number)
;(load "java-compile")
;(load "hilit-java")
;(require 'jde)
;(setq defer-loading-jde nil)

;; Xrefactory configuration part ;;
;; some Xrefactory defaults can be set here

(when (not (memq window-system '(w32)))
  (defvar xref-current-project nil) ;; can be also "my_project_name"
  (defvar xref-key-binding 'global) ;; can be also 'local or 'none
  (setq load-path (cons "/home/eiplor/.emacs.d/xref/emacs" load-path))
  (setq exec-path (cons "/home/eiplor/.emacs.d/xref" exec-path))
  (load "xrefactory")
  ;; end of Xrefactory configuration part ;;
  (message "xrefactory loaded"))

(defun my-c-mode-auto-pair-backspace (arg)
  (interactive "p")
  (if (eq (point) 0)
      (c-electric-backspace arg)
    (progn
      (backward-char-nomark arg)
      (cond ((or (looking-at "\(\)")
		 (looking-at "\\\[\\\]")
		 (looking-at "\"\""))(delete-char 2))
	    ;((looking-at "\\s[]") (delete-char 2))
	    (t (forward-char 1) (c-electric-backspace (or arg 1))))))
  ;(print arg)
  )
;(add-hook 'c-mode-common-hook 'my-c-mode-auto-del-pair)

(defun my-c-mode-auto-skip-pair (arg)
  (interactive "p")
  (self-insert-command (or arg 1))
  (backward-char)
  (cond ((or (looking-at "\)\)")
	     (looking-at "\\\]\\\]"))
	 (delete-char 1) (forward-char 1))
	(t (forward-char 1))))


(defun my-c-mode-auto-pair ()
  (interactive)
  (make-local-variable 'skeleton-pair-alist)
  (setq skeleton-pair-alist  '(
    (?\" _ "\"")
    (?` ?` _ "''")
    (?\( _ ")")
    (?\[ _ "]")
    (?{ \n > _ \n ?} >)
    ))
  (if (equal (point) (line-end-position))
      (setq skeleton-pair t))
    ;(setq skeleton-pair nil))
  (local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
  ;(local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "`") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "[") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "\"") 'skeleton-pair-insert-maybe))
(add-hook 'c-mode-common-hook 'my-c-mode-auto-pair)


;;;;;;;;;;;;;;; C & C++ Mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; c++ program mode personal define
(setq auto-mode-alist (cons '("\\.C$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.cc$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.c$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.h$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.cpp$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.cxx$" . c++-mode) auto-mode-alist))


(defun eip-c++-mode ()
  (require 'backup-dir)
  (require 'ctypes)
;  (require 'setnu-plus)
;  (setq semantic-load-turn-everything-on t)
;  (require 'semantic-load)
;  (require 'ecb)
  ;(setnu-mode)
  (linum-mode t)
  (tabbar-local-mode)
  (hs-minor-mode)
;  (hl-line-mode)
;  (glasses-mode)
  (auto-fill-mode)
;  (c-setup-filladapt)
;  (filladapt-mode 1)
;  (c-toggle-auto-hungry-state 1)
  (c-set-style "bsd")
  (setq make-backup-files t
	version-control t
	kept-old-versions 1
	kept-new-versions 10
	delete-old-versions t
	bkup-backup-directory-info '((t "~/.emacs.d/.backups/" ok-createfull-path)))
  (setq ctypes-auto-parse-mode t)
  (setq indent-tabs-mode nil)
  (setq c-ignore-auto-fill '(string))
  (setq fill-column 80)
;  (setq c-require-final-newline t)
;;;   (setq exec-path (cons "~/.emacs.d/xerf" exec-path))
;;;   (setq load-path (cons "~/.emacs.d/xref/emacs" load-path))
;;;   (load "xrefactory")
  (define-key c-mode-base-map [(control \;)] 'hs-toggle-hiding)
  (define-key c-mode-base-map [(control meta \;)] 'hs-hide-all)
  (define-key c-mode-base-map [(return)] 'newline-and-indent)
  (define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
  ;(define-key c-mode-base-map (kbd "\d") 'my-c-mode-auto-pair-backspace)
  (define-key c-mode-base-map (kbd ")") 'my-c-mode-auto-skip-pair)
  (define-key c-mode-base-map (kbd "]") 'my-c-mode-auto-skip-pair)
  ;(define-key c-mode-base-map [(tab)] 'xref-completion)
  (define-key c-mode-base-map [(meta backspace)] 'c-hungry-backspace)
  (define-key c-mode-base-map [(control backspace)] 'backward-kill-word)
  (define-key c-mode-base-map [(meta d)] 'c-hungry-delete-forward)
  )

(add-hook 'c++-mode-hook 'eip-c++-mode)
(add-hook 'c-mode-hook 'eip-c++-mode)


